module.exports = {
    state: {
        rooms: [],
        chatList: {room_idx: 0},
        chatRoom: {},
        chatUsers: {}
    },
    mutations: {
        chat (state, item) {
            state.chatRoom = item
            $router.push({name: 'chat'})
        },
        chatList (state, {id, data}) {
            lastItem = _.last(data)
            idx = lastItem ? lastItem.chat_index : state.chatList.room_idx
            if(state.chatList.list && id == state.chatList.room_id) {
                if(idx>state.chatList.room_idx) {
                    state.chatList = {
                        room_id: id,
                        room_idx: idx,
                        list: state.chatList.list.concat(data)
                    }
                }
            } else {
                state.chatList = {
                    room_id: id,
                    room_idx: idx,
                    list: data
                }
            }
        },
        chatRoom (state, {id, data}) {
            state.chatUsers = {
                room_id: id,
                list: data
            }
        }
    },
    getters: {
        chatRooms (state) {
            return state.rooms
        },
        chatRoom (state) {
            return state.chatRoom
        },
        chatUsers (state) {
            return state.chatUsers
        },
        chatList (state) {
            return state.chatList
        }
    },
    actions: {
        getRooms ({commit, state}, resolve) {
            if(!$store.getters.isLogin) {
                throw '请绑定账号'
            }
            $store.api.call('chat_rooms', {message: '获取聊天室失败'}, ({data}) => {
                if(data.length > 0){
                    state.rooms = data
                    resolve(data)
                } else {
                    throw '您没有加入任何聊天室'
                }
            })
        },
        chatSend ({commit, state}, text) {
            idx = state.chatRoom.room_id == state.chatList.room_id ? state.chatList.room_idx : 0
            $store.api.call('chat_text', {
                message: '发送聊天失败',
                form: {id: state.chatRoom.room_id, idx, text},
                noloading: true
            }, ({data}) => {
                commit('chatList', {id: state.chatRoom.room_id, data})
            })
        },
        chatList ({commit, state}, resolve) {
            idx = state.chatRoom.room_id == state.chatList.room_id ? state.chatList.room_idx : 0
            console.log('chatList',idx)
            $store.api.call('chat_list', {
                message: '获取聊天记录失败',
                form: {id: state.chatRoom.room_id, idx},
                noloading: true
            }, ({data}) => {
                commit('chatList', {id: state.chatRoom.room_id, data})
                if(resolve)
                    resolve()
            })
        },
        chatRoom ({commit, state}, resolve) {
            $store.api.call('chat_room', {message: '获取聊天用户失败', form: {id: state.chatRoom.room_id}}, ({data}) => {
                commit('chatRoom', {id: state.chatRoom.room_id, data})
                if(resolve)
                    resolve()
            })
        }
    }
}